<?php

namespace app\base\controller\admin\setting;

use app\base\model\StaffModel;

use app\BaseController;

use app\common\logic\DataLogic;



class ShopStaff extends BaseController{

    protected $name='员工';



    public function index()

    {

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0]

        ];

        $staff = new StaffModel();

        $order=['add_time'=>'desc'];

        $field='*';

        $append=['jurisdiction','add_time_format'];

        $return = DataLogic::getDataList($staff,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        $return['miniapp_status'] = $this->request->shop->status;

        return $this->success($return);

    }



    public function show()

    {

        $staff_id = $this->request->param('staff_id/d',0);

        if($staff_id==0){

            return $this->success([]);

        }

        if(null === $staff = StaffModel::find($staff_id)){

            return $this->error($this->name.'不存在');

        }

        if($staff->is_delete != 0){

            return $this->error($this->name.'不存在');

        }

        if($staff->shop_id != $this->request->shopId){

            return $this->error($this->name.'不存在');

        }

        if($staff->status != 1){

            return $this->error($this->name.'已离职');

        }

        $detail=[

            'mobile'=>$staff->mobile,

            'name'=>$staff->name,

//            'adviser_jurisdiction'=>$staff->adviserjurisdiction,

            'write_off_jurisdiction'=>$staff->write_off_jurisdiction,

            'add_value_jurisdiction'=>$staff->add_value_jurisdiction

        ];

        return $this->success(['detail'=>$detail]);

    }



    public function save()

    {

        $name=$this->request->param('name');

        $mobile=$this->request->param('mobile');

//        $adviser_jurisdiction=$this->request->param('adviser_jurisdiction/d',0);

        $write_off_jurisdiction=$this->request->param('write_off_jurisdiction/d',0);

        $add_value_jurisdiction=$this->request->param('add_value_jurisdiction/a');

        $staff_id=$this->request->param('staff_id/d',0);

        if(empty($name)){

            return $this->error($this->name.'姓名未填写');

        }



        if(empty($mobile)){

            return $this->error('手机号未填写');

        }

        if(!is_mobile($mobile)){

            return $this->error('请填写合法的手机号');

        }

        if(!in_array($write_off_jurisdiction,[0,1])){

            return $this->error('参数错误');

        }

//        if(!in_array($adviser_jurisdiction,[0,1])){

//            return $this->error('参数错误');

//        }

        if(empty($add_value_jurisdiction)){

            return $this->error('请给一点加值权限吧');

        }

        if(null !== $staff = StaffModel::find($staff_id)){

            if($staff->shop_id != $this->request->shopId){

                return $this->error('员工不存在');

            }



            if($staff->is_delete!=0){

                return $this->error($this->name.'不存在');

            }

            if($staff->status !=1){

                return $this->error($this->name.'已离职');

            }

            if($mobile!=$staff->mobile){

                $where=[

                    ['shop_id','=',$this->request->shopId],

                    ['status','=',1],

                    ['mobile','=',$mobile],

                    ['is_delete','=',0]

                ];

                if(null !== StaffModel::where($where)->find()){

                    return $this->error('该手机号已经有了');

                }

            }



        }else{

            $where=[

                ['shop_id','=',$this->request->shopId],

                ['status','=',1],

                ['mobile','=',$mobile],

                ['is_delete','=',0]

            ];

            if(null !== StaffModel::where($where)->find()){

                return $this->error('该手机号已经有了');

            }

            $staff=new StaffModel();

            $staff->shop_id=$this->request->shopId;

        }

        $staff->mobile=$mobile;

        $staff->name=$name;

//        $staff->adviser_jurisdiction=$adviser_jurisdiction;

        $staff->write_off_jurisdiction=$write_off_jurisdiction;

        $staff->add_value_jurisdiction=$add_value_jurisdiction;

        $staff->save();

        return $this->success('操作成功');

    }



    public function leave()

    {

        $staff_id = $this->request->param('staff_id/d',0);

        if(null === $staff = StaffModel::find($staff_id)){

            return $this->error($this->name.'不存在');

        }

        if($staff->is_delete != 0){

            return $this->error($this->name.'不存在');

        }

        if($staff->shop_id != $this->request->shopId){

            return $this->error($this->name.'不存在');

        }

        if($staff->status != 1){

            return $this->error($this->name.'已离职');

        }



        //判断其他的（名义下还有会员）



        $staff->status=0;

        $staff->save();

        return $this->success('操作成功');

    }



    public function choose()

    {

        $type=$this->request->param('type');

        $staff_id = $this->request->param('staff_id/d',0);

        $keyword=$this->request->param('keyword');

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

            ['status','=',1]

        ];

        if($type=='member_detail'){

            if(!empty($staff_id)) $where[]=['staff_id','<>',$staff_id];

            if(!empty($keyword)) $where[]=['name|mobile','like','%'.$keyword.'%'];

            $where[]=['adviser_jurisdiction','=',1];

        }

        $staff = new StaffModel();

        $order=['add_time'=>'desc'];

        $field='staff_id,name,mobile,add_time';

        $append=['add_time_format'];

        $return = DataLogic::getDataList($staff,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return);

    }

}

